using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

public enum EventType
{
    LevelTown1,
    LevelTown2,
    LevelTown3,
    LevelTrain,
}
public class MouseTrigger : MonoBehaviour
{

    public EventType FireEventType;

    public Button button;
    public Image Lvse;

    public int TargetScore;
    public int CurrentScore;
    private bool IsDown = false;
    private bool IsAdd = false;

    private int _AddCount;
    private float _AddFillAmount;
    private Action CompleteAction;

    private void Start()
    {
        button.onClick.AddListener(ButtonOnClick);
    }

    private int TempAddCount = 0;
    private void Update()
    {
        if (IsAdd)
        {
            CurrentScore += 1;
            TempAddCount += 1;
            Lvse.fillAmount += (1f/TargetScore);
            if (TempAddCount == _AddCount)
            {
                TempAddCount = 0;
                IsAdd = false;
                if (CurrentScore == TargetScore)
                {
                    CompleteAction?.Invoke();
                }
            }
        }
    }

    public void SetGold(Action action, int AddCount)
    {
        for (int i = 0; i < 10; i++)
        {
            GameEntry.Instance.UIGoldProgress.SetGold(gameObject.transform.position, () =>
            {
                IsAdd = true;
                CompleteAction = action;
                _AddCount = AddCount;
                // Lvse.fillAmount += AddFillAmount;
                // action?.Invoke();
            });
        }
    }

    private void ButtonOnClick()
    {
        if (IsAdd)
        {
            return;
        }
        
        if (FireEventType == EventType.LevelTown1)
        {
            LevelTown1Event levelTown1Event = new LevelTown1Event();
            EventManager.Instance.Fire(this,levelTown1Event);
            Debug.Log(111111);
        }

        if (FireEventType == EventType.LevelTown2)
        {
            LevelTown2Event levelTown2Event = new LevelTown2Event();
            EventManager.Instance.Fire(this,levelTown2Event);
        }

        if (FireEventType == EventType.LevelTown3)
        {
            LevelTown3Event levelTown3Event = new LevelTown3Event();
            EventManager.Instance.Fire(this,levelTown3Event);
        }

        if (FireEventType == EventType.LevelTrain)
        {
            LevelTrainEvent levelTrainEvent = new LevelTrainEvent();
            EventManager.Instance.Fire(this,levelTrainEvent);
        }
    }


}
